package com.milo.redis.controller;

import com.milo.redis.config.DistributedRedisLock;
import com.milo.service.IRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @author: Milogenius
 * @create: 2019-06-11 17:05
 * @description: redis的演示
 **/
@Controller
@RequestMapping("redis")
@Slf4j
public class RedisController {


    /**
     * 存储对象类型
     */
    @Autowired
    private IRedisService redisService;


    @RequestMapping("index1")
    @ResponseBody
    public String test1(){
        String s = "milo";
        redisService.set("milo", s);
        return "milo";
    }

    @RequestMapping("/redder")
    public String redder() throws IOException {

        String key = "test123";
        //加锁
        DistributedRedisLock.acquire(key);
        log.info("进入方法");
        //执行具体业务逻辑
        try {
            log.info("当前线程名称:"+Thread.currentThread().getName());
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("方法结束");
        //释放锁
        DistributedRedisLock.release(key);

        //返回结果
        return "dd";
    }
}
